package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.catering.common.seller.aop.IgnoreSeller;
import com.ruoyi.system.domain.Category;
import com.ruoyi.system.domain.dto.CategoryLevelDTO;
import com.ruoyi.system.domain.dto.CategoryListTreeDTO;
import com.ruoyi.system.mapper.CategoryMapper;
import com.ruoyi.system.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 类别服务impl
 *
 * @author Administrator
 * @date 2023/03/06
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService {

    /**
     * 获取三级分类品类
     *
     * @return 三级分类品类
     */
    @Override
    @IgnoreSeller
    public List<CategoryListTreeDTO> listTree() {
        // 获取所有品类
        List<Category> list = this
            .list(Wrappers.lambdaQuery(Category.class).eq(Category::getStatus, 1));
        // 判空
        if (CollUtil.isNotEmpty(list)) {
            List<Category> levelOneList = list.stream()
                .filter(item -> item.getCategoryLevel().equals(1)).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(levelOneList)) {
                return levelOneList.stream().map(item -> {
                    CategoryListTreeDTO treeDTO = new CategoryListTreeDTO();
                    BeanUtils.copyProperties(item, treeDTO);
                    treeDTO.setChildren(this.listSub(list, treeDTO.getCategoryId()));
                    return treeDTO;
                }).collect(Collectors.toList());
            }
        }
        return new ArrayList<>();
    }

    @Override
    @IgnoreSeller
    public List<Long> getLevelArrayInfoByChildrenId(Long category) {
        CategoryLevelDTO dto = baseMapper.getLevelArrayInfoByChildrenId(category);
        if (ObjectUtil.isNotNull(dto)) {
            List<Long> ids = new LinkedList<>();
            if (dto.getLevelOneId() != null) {
                ids.add(dto.getLevelOneId());
            }
            ids.add(dto.getLevelTwoId());
            ids.add(dto.getLevelThreeId());
            return ids;
        }
        return new ArrayList<>();
    }

    /**
     * 获取子集
     *
     * @param list     集合
     * @param parentId 父id
     * @return 子集
     */
    private List<CategoryListTreeDTO> listSub(List<Category> list, Long parentId) {
        List<Category> collect = list.stream().filter(item -> item.getParentId().equals(parentId))
            .collect(Collectors.toList());
        if (CollUtil.isNotEmpty(collect)) {
            return collect.stream().map(item -> {
                CategoryListTreeDTO treeDTO = new CategoryListTreeDTO();
                BeanUtils.copyProperties(item, treeDTO);
                treeDTO.setChildren(this.listSub(list, treeDTO.getCategoryId()));
                return treeDTO;
            }).collect(Collectors.toList());
        }
        return null;
    }
}




